package com.dianping.cat.consumer.top.model.transform;

import com.dianping.cat.consumer.top.model.IEntity;
import com.dianping.cat.consumer.top.model.IVisitor;
import com.dianping.cat.consumer.top.model.entity.Domain;
import com.dianping.cat.consumer.top.model.entity.Error;
import com.dianping.cat.consumer.top.model.entity.Machine;
import com.dianping.cat.consumer.top.model.entity.Segment;
import com.dianping.cat.consumer.top.model.entity.TopReport;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;


































public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  58 */   public DefaultXmlBuilder() { this(false); }



/*  62 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  66 */     this.m_compact = compact;
/*  67 */     this.m_sb = sb;
/*  68 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  72 */     entity.accept(this.m_visitor);
/*  73 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  77 */     this.m_level--;

/*  79 */     indent();
/*  80 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  84 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  88 */     if (value == null) {
/*  89 */       return null;
    }

/*  92 */     String str = toString(value);
/*  93 */     int len = str.length();
/*  94 */     StringBuilder sb = new StringBuilder(len + 16);

/*  96 */     for (int i = 0; i < len; i++) {
/*  97 */       char ch = str.charAt(i);

/*  99 */       switch (ch) {
        case '<':
/* 101 */           sb.append("&lt;");
          break;
        case '>':
/* 104 */           sb.append("&gt;");
          break;
        case '&':
/* 107 */           sb.append("&amp;");
          break;
        case '"':
/* 110 */           if (!text) {
/* 111 */             sb.append("&quot;");
            break;
          }
        default:
/* 115 */           sb.append(ch);
          break;
      }

    }
/* 120 */     return sb.toString();
  }

  protected void indent() {
/* 124 */     if (!this.m_compact) {
/* 125 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 126 */         this.m_sb.append("   ");
      }
    }
  }


/* 132 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 136 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 140 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 144 */     indent();

/* 146 */     this.m_sb.append('<').append(name);

/* 148 */     int len = nameValues.length;

/* 150 */     for (int i = 0; i + 1 < len; i += 2) {
/* 151 */       Object attrName = nameValues[i];
/* 152 */       Object attrValue = nameValues[i + 1];

/* 154 */       if (attrValue != null) {
/* 155 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 159 */     if (dynamicAttributes != null) {
/* 160 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 161 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 165 */     if (text != null && closed) {
/* 166 */       this.m_sb.append('>');
/* 167 */       this.m_sb.append(escape(text, true));
/* 168 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 170 */       if (closed) {
/* 171 */         this.m_sb.append('/');
      } else {
/* 173 */         this.m_level++;
      }

/* 176 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 182 */     if (value instanceof String)
/* 183 */       return (String)value; 
/* 184 */     if (value instanceof Collection) {
/* 185 */       Collection<Object> list = (Collection)value;
/* 186 */       StringBuilder sb = new StringBuilder(32);
/* 187 */       boolean first = true;

/* 189 */       for (Object item : list) {
/* 190 */         if (first) {
/* 191 */           first = false;
        } else {
/* 193 */           sb.append(',');
        }

/* 196 */         if (item != null) {
/* 197 */           sb.append(item);
        }
      }

/* 201 */       return sb.toString();
/* 202 */     }  if (value.getClass().isArray()) {
/* 203 */       int len = Array.getLength(value);
/* 204 */       StringBuilder sb = new StringBuilder(32);
/* 205 */       boolean first = true;

/* 207 */       for (int i = 0; i < len; i++) {
/* 208 */         Object item = Array.get(value, i);

/* 210 */         if (first) {
/* 211 */           first = false;
        } else {
/* 213 */           sb.append(',');
        }

/* 216 */         if (item != null) {
/* 217 */           sb.append(item);
        }
      }

/* 221 */       return sb.toString();
    }

/* 224 */     return String.valueOf(value);
  }

  protected String toString(Date date, String format) {
/* 228 */     if (date != null) {
/* 229 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 231 */     return null;
  }


  protected String toString(Number number, String format) {
/* 236 */     if (number != null) {
/* 237 */       return (new DecimalFormat(format)).format(number);
    }
/* 239 */     return null;
  }



  public void visitDomain(Domain domain) {
/* 245 */     startTag("domain", null, new Object[] { "name", domain.getName() });

/* 247 */     if (!domain.getSegments().isEmpty()) {
/* 248 */       for (Segment segment : domain.getSegments().values()) {
/* 249 */         segment.accept(this.m_visitor);
      }
    }

/* 253 */     endTag("domain");
  }



/* 258 */   public void visitError(Error error) { startTag("error", true, null, new Object[] { "id", error.getId(), "count", Integer.valueOf(error.getCount()) }); }




/* 263 */   public void visitMachine(Machine machine) { startTag("machine", true, null, new Object[] { "id", machine.getId(), "count", Integer.valueOf(machine.getCount()) }); }



  public void visitSegment(Segment segment) {
/* 268 */     startTag("segment", null, new Object[] { "id", segment.getId(), "error", Long.valueOf(segment.getError()), "url", Long.valueOf(segment.getUrl()), "url-duration", toString(Double.valueOf(segment.getUrlDuration()), "0.00"), "service", Long.valueOf(segment.getService()), "service-duration", toString(Double.valueOf(segment.getServiceDuration()), "0.00"), "sql", Long.valueOf(segment.getSql()), "sql-duration", toString(Double.valueOf(segment.getSqlDuration()), "0.00"), "call", Long.valueOf(segment.getCall()), "call-duration", toString(Double.valueOf(segment.getCallDuration()), "0.00"), "cache", Long.valueOf(segment.getCache()), "cache-duration", toString(Double.valueOf(segment.getCacheDuration()), "0.00"), "url-error", Long.valueOf(segment.getUrlError()), "url-sum", toString(Double.valueOf(segment.getUrlSum()), "0.00"), "service-error", Long.valueOf(segment.getServiceError()), "service-sum", toString(Double.valueOf(segment.getServiceSum()), "0.00"), "sql-error", Long.valueOf(segment.getSqlError()), "sql-sum", toString(Double.valueOf(segment.getSqlSum()), "0.00"), "call-error", Long.valueOf(segment.getCallError()), "call-sum", toString(Double.valueOf(segment.getCallSum()), "0.00"), "cache-error", Long.valueOf(segment.getCacheError()), "cache-sum", toString(Double.valueOf(segment.getCacheSum()), "0.00") });

/* 270 */     if (!segment.getErrors().isEmpty()) {
/* 271 */       for (Error error : segment.getErrors().values()) {
/* 272 */         error.accept(this.m_visitor);
      }
    }

/* 276 */     if (!segment.getMachines().isEmpty()) {
/* 277 */       for (Machine machine : segment.getMachines().values()) {
/* 278 */         machine.accept(this.m_visitor);
      }
    }

/* 282 */     endTag("segment");
  }


  public void visitTopReport(TopReport topReport) {
/* 287 */     startTag("top-report", null, new Object[] { "domain", topReport.getDomain(), "startTime", toString(topReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(topReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 289 */     if (!topReport.getDomains().isEmpty()) {
/* 290 */       for (Domain domain : topReport.getDomains().values()) {
/* 291 */         domain.accept(this.m_visitor);
      }
    }

/* 295 */     endTag("top-report");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/top/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */